﻿using Sirocco.Data.Helper;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using NHibernate;

namespace SiroccoTest.Data.Helper
{
    /// <summary>
    ///This is a test class for NHibernateSessionManagerTest and is intended
    ///to contain all NHibernateSessionManagerTest Unit Tests
    ///</summary>
    [TestClass]
    public class NHibernateSessionManagerTest
    {
        private TestContext TestContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get { return TestContextInstance; }
            set { TestContextInstance = value; }
        }

        #region Additional test attributes

        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //

        #endregion

        /// <summary>
        ///A test for GetSession
        ///</summary>
        [TestMethod]
        [DeploymentItem("Sirocco.dll")]
        public void GetSessionTest()
        {
            var target = new NHibernateSessionManager_Accessor();
            IInterceptor interceptor = null;
            
            var actual = target.GetSession(interceptor);
            Assert.IsNotNull(actual, "GetSession returned null");
        }

        /// <summary>
        ///A test for HasOpenTransaction
        ///</summary>
        [TestMethod]
        [DeploymentItem("Sirocco.dll")]
        public void HasOpenTransactionTest()
        {
            var target = NHibernateSessionManager_Accessor.Instance;

            // Opens a transaction
            target.GetSession();
            target.BeginTransaction();
            
            // Test must show open transaction.
            var actual = target.HasOpenTransaction();
            Assert.AreEqual(true, actual);

            target.RollbackTransaction();

            // After rollback. It must return false now.
            actual = target.HasOpenTransaction();
            Assert.AreEqual(false, actual);
        }

        /// <summary>
        ///A test for InitSessionFactory
        ///</summary>
        [TestMethod]
        [DeploymentItem("Sirocco.dll")]
        public void InitSessionFactoryTest()
        {
            var target = new NHibernateSessionManager_Accessor(); 
            target.InitSessionFactory();
        }

        /// <summary>
        ///A test for RegisterInterceptor
        ///</summary>
        [TestMethod]
        public void RegisterInterceptorTest()
        {
            var target = NHibernateSessionManager_Accessor.Instance;
            IInterceptor interceptor = null;
            target.RegisterInterceptor(interceptor);
        }

        /// <summary>
        ///A test for RollbackTransaction
        ///</summary>
        [TestMethod]
        public void RollbackTransactionTest()
        {
            var target = NHibernateSessionManager_Accessor.Instance;

            // Opens a transaction
            target.GetSession();
            target.BeginTransaction();
            target.RollbackTransaction();

            // After rollback. It must return false now.
            var actual = target.HasOpenTransaction();
            Assert.AreEqual(false, actual);
        }

        /// <summary>
        ///A test for GetSession
        ///</summary>
        [TestMethod]
        public void GetSessionTest1()
        {
            var target = NHibernateSessionManager_Accessor.Instance;
            var actual = target.GetSession();
            Assert.IsNotNull(actual, "GetSession returned null");
        }

        /// <summary>
        ///A test for GetConfiguration
        ///</summary>
        [TestMethod]
        public void GetConfigurationTest()
        {
            var target = NHibernateSessionManager_Accessor.Instance;
            var actual = target.GetConfiguration();
            Assert.IsNotNull(actual, "GetConfiguration returned null");
        }

        /// <summary>
        ///A test for CommitTransaction
        ///</summary>
        [TestMethod]
        public void CommitTransactionTest()
        {
            var target = NHibernateSessionManager_Accessor.Instance;

            // Opens a transaction
            target.GetSession();
            target.BeginTransaction();
            var actual = target.CommitTransaction();

            // After rollback. It must return false now.
            Assert.AreEqual(true, actual);
        }

        /// <summary>
        ///A test for CloseSession
        ///</summary>
        [TestMethod]
        public void CloseSessionTest()
        {
            var target = NHibernateSessionManager_Accessor.Instance;

            // Opens a transaction
            target.GetSession();
            var actual = target.HasOpenSession();

            // After rollback. It must return false now.
            Assert.AreEqual(true, actual);

            target.CloseSession();
            actual = target.HasOpenSession();

            // After rollback. It must return false now.
            Assert.AreEqual(false, actual);
        }

        /// <summary>
        ///A test for BeginTransaction
        ///</summary>
        [TestMethod]
        public void BeginTransactionTest()
        {
            var target = NHibernateSessionManager_Accessor.Instance;

            // Opens a transaction
            target.GetSession();
            target.BeginTransaction();
            
            // After rollback. It must return false now.
            var actual = target.HasOpenTransaction();
            Assert.AreEqual(true, actual);
        }

        /// <summary>
        ///A test for NHibernateSessionManager Constructor
        ///</summary>
        [TestMethod]
        [DeploymentItem("Sirocco.dll")]
        public void NHibernateSessionManagerConstructorTest()
        {
            var target = new NHibernateSessionManager_Accessor();
        }
    }
}